﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Bussines;

namespace Bussines
{
    public class clFIGYELEM
    {
        private SqlConnection sc;

        public DataSet dsFIGYELEM = new DataSet();
        public DataSet dsFN_FIGYELEM = new DataSet();

        SqlDataAdapter adapterFIGYELEM;
        SqlDataAdapter adapterFN_FIGYELEM;

        private string SQL = "SELECT FIGYELEM.*, PRT_NEV1 + ' ' + PRT_NEV2 AS PRT_NEV " +
                "FROM FIGYELEM " +
                "INNER JOIN PARTNER ON PRT_ID = FGY_PRT_ID ";

        public clFIGYELEM(TBLObj iBLObj)
        {
            TBLObj pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;
        }
        public void ExecSP_FIGYELEM(int iUSR_ID, DateTime iDATUM)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = sc;

            cmd.CommandType = CommandType.Text;
            
            cmd.CommandText = "exec SP_FIGYELEM " + iUSR_ID.ToString() + " , @date, 0 ";

            SqlParameter myParam = new SqlParameter();
            myParam.ParameterName = "@date";
            myParam.SqlDbType = SqlDbType.DateTime;
            myParam.Value = iDATUM;
            cmd.Parameters.Add(myParam);

           
            cmd.ExecuteNonQuery();
        }
        public int FilldsFIGYELEM(int iUSR_ID)
        {
            adapterFIGYELEM = new SqlDataAdapter();

            adapterFIGYELEM.SelectCommand = new SqlCommand(SQL +
                "WHERE FGY_USR_ID = " + iUSR_ID.ToString() + 
                " AND FGY_STATUS = '0'" , sc);
            if (dsFIGYELEM.Tables["FIGYELEM"] != null)
                dsFIGYELEM.Tables["FIGYELEM"].Clear();

            adapterFIGYELEM.Fill(dsFIGYELEM, "FIGYELEM");
            
            
            return dsFIGYELEM.Tables["FIGYELEM"].Rows.Count;
        }
        public void SetOk(int iFGY_ID)
        {
            SqlCommand cmd = new SqlCommand();
            
            cmd.Connection = sc;
            
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE FIGYELEM SET " +
                " FGY_STATUS = '1' " +
                " WHERE FGY_ID = " + iFGY_ID.ToString();

            cmd.ExecuteNonQuery();
        }

        public int FilldsFN_FIGYELEM(int iUSR_ID, DateTime iDatum)
        {
            adapterFN_FIGYELEM = new SqlDataAdapter();

            
            adapterFN_FIGYELEM.SelectCommand = new SqlCommand("SELECT * FROM FN_FIGYELEM " +
                "(@USR_ID , @DATUM)",sc);
            
            adapterFN_FIGYELEM.SelectCommand.Parameters.Add(new SqlParameter("USR_ID", SqlDbType.Int));
            adapterFN_FIGYELEM.SelectCommand.Parameters.Add(new SqlParameter("DATUM", SqlDbType.DateTime));

            adapterFN_FIGYELEM.SelectCommand.Parameters["USR_ID"].Value = iUSR_ID;
            adapterFN_FIGYELEM.SelectCommand.Parameters["DATUM"].Value = iDatum;

            if (dsFN_FIGYELEM.Tables["FN_FIGYELEM"] != null)
                dsFN_FIGYELEM.Tables["FN_FIGYELEM"].Clear();

            adapterFN_FIGYELEM.Fill(dsFN_FIGYELEM, "FN_FIGYELEM");


            return dsFN_FIGYELEM.Tables["FN_FIGYELEM"].Rows.Count;
        }
    }
}
